package com.binhthuan.catechist.client.data;

import com.binhthuan.catechist.shared.constant.Parameter;
import com.binhthuan.catechist.shared.constant.Service;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.form.fields.SelectItem;

public class ScheduleDS extends AbstractDS {

    private static ScheduleDS instance;

    private ScheduleDS() {
        super(Service.SCHEDULE);

        DataSourceIntegerField id = new DataSourceIntegerField(Parameter.ID);
        id.setHidden(true);
        id.setPrimaryKey(true);
        id.setRequired(true);

        DataSourceIntegerField doctrineClassId = new DataSourceIntegerField(Parameter.COURSE);
        doctrineClassId.setHidden(true);
        doctrineClassId.setRequired(true);
        doctrineClassId.setValueXPath(combineXPath(Parameter.COURSE, Parameter.ID));
        
        DataSourceTextField title = new DataSourceTextField(Parameter.TITLE);
        
        SelectItem dateEditor = new SelectItem(Parameter.DATE);
        dateEditor.setValueField(Parameter.ID);
        dateEditor.setDisplayField(Parameter.DESCRIPTION);
        dateEditor.setOptionDataSource(DateDS.getInstance());
        dateEditor.setWidth(250);
        dateEditor.setDefaultToFirstOption(true);

        DataSourceTextField scheduleDate = new DataSourceTextField(Parameter.DATE);
        scheduleDate.setRequired(true);
        scheduleDate.setValueXPath(combineXPath(Parameter.DATE, Parameter.ID));
        scheduleDate.setTypeAsDataSource(DateDS.getInstance());
        scheduleDate.setEditorType(dateEditor);

        SelectItem statusEditor = new SelectItem(Parameter.STATUS);
        statusEditor.setValueField(Parameter.ID);
        statusEditor.setDisplayField(Parameter.NAME);
        statusEditor.setOptionDataSource(ScheduleStatusDS.getInstance());
        statusEditor.setWidth(250);
        statusEditor.setDefaultToFirstOption(true);

        DataSourceTextField status = new DataSourceTextField(Parameter.STATUS);
        status.setRequired(true);
        status.setValueXPath(combineXPath(Parameter.STATUS, Parameter.ID));
        status.setTypeAsDataSource(ScheduleStatusDS.getInstance());
        status.setEditorType(statusEditor);

        DataSourceTextField content = new DataSourceTextField(Parameter.CONTENT);
        content.setRequired(true);

        DataSourceTextField comment = new DataSourceTextField(Parameter.COMMENT);

        DataSourceDateField dateOfYear = new DataSourceDateField(Parameter.DATE_OF_YEAR);
        dateOfYear.setValueXPath(combineXPath(Parameter.DATE, Parameter.DATE_OF_YEAR));

        DataSourceTextField liturgical = new DataSourceTextField(Parameter.LITURGICAL_DATE);
        liturgical.setValueXPath(combineXPath(Parameter.DATE, Parameter.LITURGICAL_DATE));

        DataSourceTextField statusName = new DataSourceTextField(Parameter.STATUS_NAME);
        statusName.setValueXPath(combineXPath(Parameter.STATUS, Parameter.NAME));

        setFields(id, doctrineClassId, scheduleDate, title, content, comment, status, dateOfYear, liturgical, statusName);
    }

    /**
     * @return the instance
     */
    public static ScheduleDS getInstance() {
        if (instance == null) {
            instance = new ScheduleDS();
        }

        return instance;
    }
}
